package com.tuoyu.replace.tool;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 项目改造配置类
 * 从配置文件读取改造参数
 * 
 * @author tuoyu
 */
public class RefactorConfig {
    
    private Properties properties;
    
    // 默认配置
    private static final String DEFAULT_SOURCE_PROJECT = "D:\\project\\gimiii\\v2\\interface-platform";
    private static final String DEFAULT_TARGET_BASE_PATH = "D:\\project\\gimiii\\v2\\interface-platform-refactored";
    private static final String DEFAULT_PROJECT_PREFIX = "interface";
    private static final String DEFAULT_GROUP_ID = "com.yaoyi";
    private static final String DEFAULT_VERSION = "1.0.0";
    
    public RefactorConfig() {
        this.properties = new Properties();
        loadConfig();
    }
    
    /**
     * 从配置文件加载配置
     */
    private void loadConfig() {
        try (InputStream input = getClass().getClassLoader()
                .getResourceAsStream("refactor-config.properties")) {
            if (input != null) {
                properties.load(input);
                System.out.println("✓ 已加载配置文件: refactor-config.properties");
            } else {
                System.out.println("⚠ 配置文件不存在，使用默认配置");
                loadDefaultConfig();
            }
        } catch (IOException e) {
            System.out.println("⚠ 读取配置文件失败，使用默认配置");
            loadDefaultConfig();
        }
    }
    
    /**
     * 加载默认配置
     */
    private void loadDefaultConfig() {
        properties.setProperty("source.project.path", DEFAULT_SOURCE_PROJECT);
        properties.setProperty("target.project.path", DEFAULT_TARGET_BASE_PATH);
        properties.setProperty("project.prefix", DEFAULT_PROJECT_PREFIX);
        properties.setProperty("project.groupId", DEFAULT_GROUP_ID);
        properties.setProperty("project.version", DEFAULT_VERSION);
        properties.setProperty("java.version", "21");
        properties.setProperty("spring.boot.version", "3.5.7");
        properties.setProperty("spring.cloud.version", "2023.0.0");
        properties.setProperty("spring.cloud.alibaba.version", "2022.0.0.0");
        properties.setProperty("copy.resources", "true");
        properties.setProperty("generate.pom", "true");
        properties.setProperty("generate.config", "true");
        properties.setProperty("generate.report", "true");
        properties.setProperty("nacos.server.addr", "localhost:8848");
        properties.setProperty("gateway.port", "9200");
        properties.setProperty("system.module.port", "9201");
        properties.setProperty("auth.module.port", "9202");
    }
    
    // Getter方法
    
    public String getSourceProjectPath() {
        return properties.getProperty("source.project.path", DEFAULT_SOURCE_PROJECT);
    }
    
    public String getTargetProjectPath() {
        return properties.getProperty("target.project.path", DEFAULT_TARGET_BASE_PATH);
    }
    
    public String getProjectPrefix() {
        return properties.getProperty("project.prefix", DEFAULT_PROJECT_PREFIX);
    }
    
    public String getGroupId() {
        return properties.getProperty("project.groupId", DEFAULT_GROUP_ID);
    }
    
    public String getVersion() {
        return properties.getProperty("project.version", DEFAULT_VERSION);
    }
    
    public String getJavaVersion() {
        return properties.getProperty("java.version", "21");
    }
    
    public String getSpringBootVersion() {
        return properties.getProperty("spring.boot.version", "3.5.7");
    }
    
    public String getSpringCloudVersion() {
        return properties.getProperty("spring.cloud.version", "2023.0.0");
    }
    
    public String getSpringCloudAlibabaVersion() {
        return properties.getProperty("spring.cloud.alibaba.version", "2022.0.0.0");
    }
    
    public boolean isCopyResources() {
        return Boolean.parseBoolean(properties.getProperty("copy.resources", "true"));
    }
    
    public boolean isGeneratePom() {
        return Boolean.parseBoolean(properties.getProperty("generate.pom", "true"));
    }
    
    public boolean isGenerateConfig() {
        return Boolean.parseBoolean(properties.getProperty("generate.config", "true"));
    }
    
    public boolean isGenerateReport() {
        return Boolean.parseBoolean(properties.getProperty("generate.report", "true"));
    }
    
    public String getNacosServerAddr() {
        return properties.getProperty("nacos.server.addr", "localhost:8848");
    }
    
    public int getGatewayPort() {
        return Integer.parseInt(properties.getProperty("gateway.port", "9200"));
    }
    
    public int getSystemModulePort() {
        return Integer.parseInt(properties.getProperty("system.module.port", "9201"));
    }
    
    public int getAuthModulePort() {
        return Integer.parseInt(properties.getProperty("auth.module.port", "9202"));
    }
    
    /**
     * 打印当前配置
     */
    public void printConfig() {
        System.out.println("\n========================================");
        System.out.println("当前配置信息");
        System.out.println("========================================");
        System.out.println("源项目路径: " + getSourceProjectPath());
        System.out.println("目标路径: " + getTargetProjectPath());
        System.out.println("项目前缀: " + getProjectPrefix());
        System.out.println("组ID: " + getGroupId());
        System.out.println("版本: " + getVersion());
        System.out.println("----------------------------------------");
        System.out.println("Java版本: " + getJavaVersion());
        System.out.println("Spring Boot: " + getSpringBootVersion());
        System.out.println("Spring Cloud: " + getSpringCloudVersion());
        System.out.println("Spring Cloud Alibaba: " + getSpringCloudAlibabaVersion());
        System.out.println("----------------------------------------");
        System.out.println("复制资源文件: " + (isCopyResources() ? "是" : "否"));
        System.out.println("生成POM文件: " + (isGeneratePom() ? "是" : "否"));
        System.out.println("生成配置文件: " + (isGenerateConfig() ? "是" : "否"));
        System.out.println("生成报告: " + (isGenerateReport() ? "是" : "否"));
        System.out.println("----------------------------------------");
        System.out.println("Nacos地址: " + getNacosServerAddr());
        System.out.println("网关端口: " + getGatewayPort());
        System.out.println("业务模块端口: " + getSystemModulePort());
        System.out.println("认证模块端口: " + getAuthModulePort());
        System.out.println("========================================\n");
    }
    
    /**
     * 验证配置
     */
    public boolean validateConfig() {
        boolean valid = true;
        
        if (getSourceProjectPath() == null || getSourceProjectPath().isEmpty()) {
            System.err.println("❌ 源项目路径不能为空");
            valid = false;
        }
        
        if (getTargetProjectPath() == null || getTargetProjectPath().isEmpty()) {
            System.err.println("❌ 目标路径不能为空");
            valid = false;
        }
        
        if (getProjectPrefix() == null || getProjectPrefix().isEmpty()) {
            System.err.println("❌ 项目前缀不能为空");
            valid = false;
        }
        
        if (getSourceProjectPath().equals(getTargetProjectPath())) {
            System.err.println("❌ 源路径和目标路径不能相同");
            valid = false;
        }
        
        return valid;
    }
    
    /**
     * 测试方法
     */
    public static void main(String[] args) {
        RefactorConfig config = new RefactorConfig();
        config.printConfig();
        
        if (config.validateConfig()) {
            System.out.println("✓ 配置验证通过");
        } else {
            System.out.println("✗ 配置验证失败");
        }
    }
}

